using System;
using System.Collections.Generic;
using System.Text;

using Weazel.Microblaze.Simulator.Engine;

namespace Weazel.Microblaze.Simulator.Components.Microblaze.Instructions
{
    /// <summary>
    /// Branch If Not Equal
    /// </summary>
    public class Bne
      : ConditionalBranchInstruction
    {
        private enum BneInstructionKind
        {
            None,   // error
            bne,    // branch if not equal
            bned    // branch if not equal with delay
        }

        private BneInstructionKind kind;

        public override int CycleCount
        {
            get
            {
                if (BranchStatus == BranchInstructionCycleCountStatus.BranchTaken)
                    return 1;
                else
                    return kind == BneInstructionKind.bned ? 2 : 3;
            }
        }
        public Bne()
            : base("bne")
        {
        }


        public override void Set(uint address, uint value)
        {
            base.Set(address, value);

            kind =
              DestinationRegister == 0x11 ? BneInstructionKind.bned :
              DestinationRegister == 0x01 ? BneInstructionKind.bne :
              BneInstructionKind.None;

            if (kind == BneInstructionKind.None)
                throw new UnknownInstructionException(value);
        }

        /// <summary>
        /// Branch if rA not equal to 0, to the instruction located in the offset value 
        /// of rB. The target of the branch will be the instruction at address PC + rB.
        /// The mnemonic bned will set the D bit. The D bit determines whether there is 
        /// a branch delay slot or not. If the D bit is set, it means that there is a 
        /// delay slot and the instruction following the branch (i.e. in the branch 
        /// delay slot) is allowed to complete execution before executing the target 
        /// instruction. If the D bit is not set, it means that there is no delay slot, 
        /// so the instruction to be executed after the branch is the target instruction.
        /// </summary>    
        public override void Execute(MicroblazeComponent MicroblazeComponent)
        {
            uint sa = MicroblazeComponent.ReadRegister(SourceRegisterA);
            uint sb = MicroblazeComponent.ReadRegister(SourceRegisterB);

            if (sa != 0)
            {
                MicroblazeComponent.Pc = MicroblazeComponent.Pc + sb;
                BranchStatus = BranchInstructionCycleCountStatus.BranchTaken;

                if (kind == BneInstructionKind.bned)
                    MicroblazeComponent.DelayedBranchActive = true;
            }
            else
            {
                MicroblazeComponent.Pc += 4;
                BranchStatus = BranchInstructionCycleCountStatus.BranchNotTaken;
            }
        }

        public override string ToString()
        {
            return string.Format("{0} r{1}, r{2}",
              kind,
              SourceRegisterA,
              SourceRegisterB
            );
        }
    }
}
